﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LevelEditor.Dao;


namespace LevelEditor
{
    public partial class NewCondition : Form
    {

        private List<Condition> CurrentCondition = new List<Condition>();
        private int CurrentSelected { get; set; }

        public NewCondition()
        {
            InitializeComponent();
        }

        private Label GetTitle()
        {
            Label title = new Label();
            title.Text = "Current Conditions";
            title.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
            //title.TextAlign = ContentAlignment.TopLeft;

            return title;
        }

        protected override void OnLoad(EventArgs e)
        {
            tablePanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
            //tablePanel.Controls.Add(GetTitle(), 0, 0);
            UpdateBehaviors();
            base.OnLoad(e);
        }

        private void RemoveBehaviors()
        {
            tablePanel.Controls.Clear();
        }

        private void UpdateBehaviors()
        {
            CurrentCondition = LevelEditorDAO.GetInstance().GetAllCondition();

            tablePanel.RowCount = CurrentCondition.Count + 1;
            tablePanel.Controls.Add(GetTitle(), 0, 0);
            for (int i = 0; i < CurrentCondition.Count; i++)
            {
                Label l = new Label();
                l.Enabled = false;
                l.Text = CurrentCondition[i].Name;

                tablePanel.Controls.Add(l, 0, i+1);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtNameCondition.Text))
            {
                MessageBox.Show("Preencha o nome da nova condição");
                return;
            }
            else
            {
                try
                {
                    if (LevelEditorDAO.GetInstance().SaveCondition(txtNameCondition.Text))
                    {
                        
                        txtNameCondition.Clear();
                        RemoveBehaviors();
                        UpdateBehaviors();
                    }
                }
                catch(Exception){ }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (CurrentSelected <= 0 || CurrentSelected > CurrentCondition.Count) return;
            //MessageBox.Show("trigger "+CurrentTriggers[CurrentSelected - 1].Name+"  --  "+CurrentSelected);
            if (MessageBox.Show("Deseja apagar esta condição?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                //MessageBox.Show("trigger "+CurrentTriggers[CurrentSelected - 1].Name+"  --  "+CurrentSelected);
                LevelEditorDAO.GetInstance().DeleteCondition(CurrentCondition[CurrentSelected - 1]);
                ClearSelected();
                RemoveBehaviors();
                UpdateBehaviors();
            }
        }

        private void SelectTrigger(object sender, EventArgs e)
        {
            Point p = Util.Util.GetPointInTablePanel(tablePanel, tablePanel.PointToClient(Cursor.Position));
            if (p.IsEmpty || p.Y == 0)
            {
                ClearSelected();
                return;
            }
            CurrentSelected = p.Y; //Row selected

            for (int row = 0; row < tablePanel.RowCount; row++)
            {
                Control control = tablePanel.GetControlFromPosition(0, row);
                if (row == CurrentSelected)
                    control.BackColor = Color.Blue;
                else
                    control.BackColor = Color.Transparent;
            }

        }

        private void ClearSelected()
        {
            if (CurrentSelected <= 0) return;
            tablePanel.GetControlFromPosition(0, CurrentSelected).BackColor = Color.Transparent;
            CurrentSelected = -1;
            /*for (int i = 0; i < tbDialogs.ColumnCount; i++)
            {
                Control control = tbDialogs.GetControlFromPosition(i, currentSelected);
                control.BackColor = Color.Transparent;
            }*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClearSelected();
        }

    }
}
